/* ------------------------------ plugins.js -------------------------------- */
VED.plugins = new VED.resourceManager( 'plugins/', 'plugin' );

VED.plugins.load = VED.tools.override( VED.plugins.load, function( originalLoad ) {
   var initialized = {};

   return function( name, callback, scope ) {
      var allPlugins = {};

      var loadPlugins = function( names ) {
            originalLoad.call( this, names, function( plugins ) {
               VED.tools.extend( allPlugins, plugins );

               var requiredPlugins = [];
               for ( var pluginName in plugins ) {
                  var plugin = plugins[ pluginName ],
                     requires = plugin && plugin.requires;

                  if ( !initialized[ pluginName ] ) {
                     // Register all icons eventually defined by this plugin.
                     if ( plugin.icons ) {
                        var icons = plugin.icons.split( ',' );
                        for ( var ic = icons.length; ic--; ) {
                           VED.skin.addIcon( icons[ ic ], 
                              plugin.path +
                              'icons/' +
                              ( VED.env.hidpi && plugin.hidpi ? 'hidpi/' : '' ) +
                              icons[ ic ] +
                              '.png' );
                        }
                     }
                     initialized[ pluginName ] = 1;
                  }

                  if ( requires ) {
                     // Trasnform it into an array, if it's not one.
                     if ( requires.split )
                        requires = requires.split( ',' );

                     for ( var i = 0; i < requires.length; i++ ) {
                        if ( !allPlugins[ requires[ i ] ] )
                           requiredPlugins.push( requires[ i ] );
                     }
                  }
               }

               if ( requiredPlugins.length )
                  loadPlugins.call( this, requiredPlugins );
               else {
                  // Call the "onLoad" function for all plugins.
                  for ( pluginName in allPlugins ) {
                     plugin = allPlugins[ pluginName ];
                     if ( plugin.onLoad && !plugin.onLoad._called ) {
                        // Make it possible to return false from plugin::onLoad to disable it.
                        if ( plugin.onLoad() === false )
                           delete allPlugins[ pluginName ];

                        plugin.onLoad._called = 1;
                     }
                  }

                  // Call the callback.
                  if ( callback )
                     callback.call( scope || window, allPlugins );
               }
            }, this );

         };

      loadPlugins.call( this, name );
   };
});

VED.plugins.setLang = function( pluginName, languageCode, languageEntries ) {
   var plugin = this.get( pluginName ),
      pluginLangEntries = plugin.langEntries || ( plugin.langEntries = {} ),
      pluginLang = plugin.lang || ( plugin.lang = [] );

   if ( pluginLang.split )
      pluginLang = pluginLang.split( ',' );

   if ( VED.tools.indexOf( pluginLang, languageCode ) == -1 )
      pluginLang.push( languageCode );

   pluginLangEntries[ languageCode ] = languageEntries;
};





